package sale.wxb.loveshopping.controller.permission;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import sale.wxb.loveshopping.entity.common.Result;
import sale.wxb.loveshopping.entity.model.SysMenu;
import sale.wxb.loveshopping.entity.vo.common.PageListParam;
import sale.wxb.loveshopping.entity.vo.common.PageRecord;
import sale.wxb.loveshopping.entity.vo.menu.SaveMenuVo;
import sale.wxb.loveshopping.enums.MenuTypeEnum;
import sale.wxb.loveshopping.enums.ResponseEnum;
import sale.wxb.loveshopping.service.ISysMenuService;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import java.util.List;

@RestController
@RequestMapping("sys-menu")
@Api(tags = "系统菜单")
@Validated
public class SysMenuController {
    @Autowired
    private ISysMenuService menuService;

    @ApiOperation(value = "保存菜单", notes = "新增或更新菜单")
    @PostMapping("save")
    public Result<Long> save(@Validated @RequestBody SaveMenuVo body) {
        if (body.getParentId() != null && body.getParentId() != 0 && ObjectUtils.isEmpty(menuService.getById(body.getParentId()))) {
            return Result.fail("上级菜单不存在");
        }
        // 保存菜单时 前端路由、组件路径必填
        if (body.getMenuType().equals(MenuTypeEnum.MENU.getCode())) {
            if (!StringUtils.hasText(body.getFrontEndRouter())) {
                return Result.fail("前端路由不能为空");
            }
            if (!StringUtils.hasText(body.getComponentPath())) {
                return Result.fail("前端组件路径不能为空");
            }
        }
        Long menuId = menuService.saveMenu(body);
        return menuId != null ? Result.success(menuId) : Result.fail();
    }

    @ApiOperation(value = "按id删除菜单", notes = "存在下级菜单时无法删除")
    @GetMapping("delete")
    public Result<Boolean> deleteById(@RequestParam @NotNull @Positive Long id) {
        if (menuService.hasChildrenMenuById(id).size() > 0) {
            return Result.fail("存在下级菜单,不允许删除");
        }
        return Result.send(menuService.removeById(id));
    }

    @ApiOperation("获取菜单列表")
    @GetMapping("list")
    public Result<PageRecord<SysMenu>> list(@Validated PageListParam listParam) {
        return Result.success(menuService.menuListByPage(listParam));
    }

    @ApiOperation("按id查询菜单详情")
    @GetMapping("findById")
    public Result<SysMenu> findById(@RequestParam @NotNull @Positive Long id) {
        SysMenu sysMenu = menuService.getById(id);
        return ObjectUtils.isEmpty(sysMenu) ? Result.fail(ResponseEnum.NOT_FOUND) : Result.success(sysMenu);
    }
    
    @ApiOperation("根据id查询下级菜单列表")
    @GetMapping("findMenuChildrenById")
    public Result<List<SysMenu>> findMenuChildrenById(@RequestParam @NotNull @Positive Long id) {
        return Result.success(menuService.hasChildrenMenuById(id));
    }
}
